/*
	新增视频
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer;
		var form = layui.form;

		// wang editor 富文本编辑器
		var WEditor = window.wangEditor,
            editorCn = new WEditor('#htmlCn'),
            editorTw = new WEditor('#htmlTw'),
            editorEn = new WEditor('#htmlEn');
        editorCn.create();
        editorTw.create();
        editorEn.create();

        //ajax data
        var ajaxData = {
        	aId: adminLoginedInfo.aId,
        	aToken : adminLoginedInfo.aToken
        };
        //重置表单
        $('[lay-filter="reset"]').on('click', function(){
		    layer.confirm(MESSAGES.reset_form, { btn:['确认','取消'] }, function(index){  //确认
		        location.reload();
		        layer.close(index);
		    });
		});
		function addNewVideo(video_data){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var _ajaxData = $.extend({}, video_data, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.video,
				type: 'PUT',
				data: _ajaxData,
				success: function(responseData){
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						//console.log(responseData)
						layer.msg(responseData.data.msg, {time:2000, end: function(){
							//关闭当前窗口并刷新父窗口
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					};
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}});
				}
			})
		}
        //提交表单
        form.on('submit(newAdd)', function(data){
        	layer.confirm(MESSAGES.confirm_add, {title: '提示'}, function(index){
        		//console.log(data)
        		var field = data.field;
        		var video_data = {
        			title: langTransObj(field.titleCn, field.titleTw, field.titleEn),
        			status: field.status,
        			link: langTransObj(field.videoCn, field.videoTw, field.videoEn),
        			img: field.videoImg,
        			cn: editorCn.txt.html(),
                	tw: editorTw.txt.html(),
                	en: editorEn.txt.html()
        		};
        		//新增
        		addNewVideo(video_data)
        		layer.close(index);
        	})
        })








	});
})







